package testClasses.xstream;

import utilities.Key;
import model.AbstractModel;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import controller.AbstractController;

public class ModelConverter implements Converter {

	public boolean canConvert(Class clazz) {
		return AbstractModel.class.isAssignableFrom(clazz);
	}

	public void marshal(Object obj, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		
		System.out.println("Marshalling Model");
		
		AbstractModel model = (AbstractModel) obj;
		Key key = model.getKey();
		writer.startNode("key");
		writer.setValue(key.getValue()+"");
		writer.endNode();
						
	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		
		System.out.println("UNMarshalling Model");
		
		AbstractModel model = new AbstractModel();		
		reader.moveDown();
		model.setKey(new Key(Integer.parseInt(reader.getValue())));
		reader.moveUp();
		
		return model;
	}
	
}
